package com.sixmac.util;


import com.sixmac.core.toolbox.kit.HttpKit;
import com.sixmac.core.toolbox.kit.JsonKit;

import java.util.HashMap;
import java.util.regex.Pattern;

/**
 *
 */
public class LockUtil {

//    static String url = "https://devchat.leocheery.net";
    static String url = "https://appcon.leocheery.net/device-platform/";
    static String url2 = "https://devchat.leocheery.net/oauthclient-platform";
    static String companyToken = "/auth-platform/oauth/token";//请求CompanyToken
    static String auth = "/device/";//请求授权
    static String clientId = "APP0016CD24A106C7D6";
    static String clientSecret = "9583CDA08A2B538482CD2BE831C31D2C";
    static String requestScene ="/auth/requestScene/";

    public static void main(String[] args) {
        LockUtil lockUtil = new LockUtil();
//        lockUtil.authDeviceUser("A014176C25011672","2018-08-29 09:00","2018-08-29 18:00");
        lockUtil.token();
    }

    public  HashMap token(){
        HashMap<String,Object> params = new HashMap<>();
        params.put("client_id",clientId);
        params.put("client_secret",clientSecret);
        params.put("scope","read+write");
        params.put("grant_type","client_credentials");
        String param = "client_id="+clientId+"&client_secret="+clientSecret+"&scope=read+write&grant_type=client_credentials";
        String uri=url+companyToken;
        try {
            String result = HttpKit.post(uri,params);
            HashMap data = JsonKit.parse(result, HashMap.class);
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public String authDeviceUser(String lock,String startDay,String endDay){
//            Dept dept = Blade.create(Dept.class).findById(62);
//            if (dept == null){
//                return null;
//            }
////            if (dept.getLockToken() == null||dept.getLockToken().trim().length()==0){
//                HashMap hashMap = token();
//                String token = hashMap.get("access_token").toString();
//                dept.setLockToken(token);
//                dept.setTokenTime(new Date());
//                Blade.create(Dept.class).update(dept);
////            }
////            long diff = (new Date().getTime() - dept.getTokenTime().getTime())/(1000*60);
////            if (diff>=40){
////                HashMap hashMap = token();
////                String token = hashMap.get("access_token").toString();
////                dept.setLockToken(token);
////                dept.setTokenTime(new Date());
////                Blade.create(Dept.class).update(dept);
////            }
//            HashMap<String,String> params = new HashMap<>();
//            params.put("appId","wx8f268b7d0a93ab41");
//            params.put("startDay",startDay);
//            params.put("endDay",endDay);
//            String uri=url2+auth+lock+"/authDeviceUser";
//        try {
//            String result = HttpClientKit.delete(uri,dept.getLockToken());
//            System.out.println(result);
//            result = HttpClientKit.post(uri, params,"utf8",dept.getLockToken());
//            JSONObject data = JsonKit.parse(result);
//            String authUrl=url+"/open-platform"+requestScene+data.getJSONObject("data").get("ticket").toString();
//            try {
//                String fileName = UploadUtil.getImageCodeUpladPath() + System.currentTimeMillis() + "_" + DateKit.getDays() + ".png";
////                String url = "d:/mpl.jpg";
//                boolean temp = HttpClientKit.getFile(authUrl,"utf8",dept.getLockToken(), ConstConfig.UPLOADFILE_PATH + fileName);
//                if (temp){
//                    return fileName;
//                }else{
//                    return null;
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        } catch (KeyManagementException e) {
//            e.printStackTrace();
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        return null;
    }
}
